package com.zzl.alligator.management.system.controller;

import com.zzl.alligator.common.enums.BusinessTypeEnum;
import com.zzl.alligator.common.util.ExcelUtils;
import com.zzl.alligator.framework.aspectj.annotation.Log;
import com.zzl.alligator.framework.base.controller.BaseController;
import com.zzl.alligator.framework.support.web.vo.JsonResult;
import com.zzl.alligator.management.system.domain.SysPost;
import com.zzl.alligator.management.system.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 岗位信息表(SysPost)控制层
 *
 * @author zhouziliang
 * @since 2020-07-01
 */
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {
    
    /**
     * 服务对象
     */
    @Autowired
    private ISysPostService sysPostService;

    /**
     * 分页查询所有数据
     *
     * @param sysPost 查询实体
     * @return 所有数据
     */
    @GetMapping
    public JsonResult findList(SysPost sysPost) {
        startPage();
        List<SysPost> sysPostList = sysPostService.findSysPostList(sysPost);
        return JsonResult.success(getDataTable(sysPostList));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param postId 主键
     * @return 单条数据
     */
    @GetMapping("/{postId}")
    public JsonResult findById(@PathVariable Long postId) {
        return JsonResult.success(sysPostService.findSysPostById(postId));
    }

    /**
     * 新增数据
     *
     * @param sysPost 实体对象
     * @return 新增结果
     */
    @PostMapping
    @Log(title = "新增岗位信息", businessType = BusinessTypeEnum.INSERT)
    public JsonResult add(@RequestBody SysPost sysPost) {
        return JsonResult.success(this.sysPostService.insertSysPost(sysPost));
    }

    /**
     * 修改数据
     *
     * @param sysPost 实体对象
     * @return 修改结果
     */
    @PutMapping
    @Log(title = "更新岗位信息", businessType = BusinessTypeEnum.UPDATE)
    public JsonResult update(@RequestBody SysPost sysPost) {
        return JsonResult.success(this.sysPostService.updateSysPost(sysPost));
    }

    /**
     * 删除数据
     *
     * @param postId 主键
     * @return 删除结果
     */
    @DeleteMapping("/{postId}")
    @Log(title = "删除岗位信息", businessType = BusinessTypeEnum.DELETE)
    public JsonResult delete(@PathVariable Long postId) {
        return JsonResult.success(this.sysPostService.deleteById(postId));
    }

    /**
     * 导出数据
     *
     * @param role 条件
     * @return 导出文件路径
     */
    @GetMapping("/export")
    @Log(title = "导出岗位信息", businessType = BusinessTypeEnum.EXPORT)
    public JsonResult export(SysPost post) {
        List<SysPost> list = sysPostService.findSysPostList(post);
        ExcelUtils<SysPost> util = new ExcelUtils<>(SysPost.class);
        return util.exportExcel(list, "岗位数据");
    }
}